package cn.dnuifood_cjy.dnuifood_16110100529.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnuifood_cjy.dnuifood_16110100529.JavaBean.UserBean;
import cn.dnuifood_cjy.dnuifood_16110100529.R;
import cn.dnuifood_cjy.dnuifood_16110100529.listener.OnRetrofitListener;
import cn.dnuifood_cjy.dnuifood_16110100529.model.UserModel;


public class LoginActivity extends BaseActivity
        implements OnRetrofitListener<UserBean> {
    private EditText username;
    private EditText userpass;
    private Button btn_login;
    private Button btn_reg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_login);

    }

    @Override
    void initViews() {
        setLayout(R.layout.activity_login);
        username=(EditText)findViewById(R.id.username);
        userpass=(EditText)findViewById(R.id.userpass);
        btn_login=(Button)findViewById(R.id.button_login);
        btn_reg=(Button)findViewById(R.id.button_register);


    }

    @Override
    void initEvents() {
        // UserModel model=new UserModel();
        //  model.login("","",this);
//        btn_login.setOnClickListener(this);
//        btn_reg.setOnClickListener(this);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().trim().equals("")||userpass.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else{
                    login();
                }

            }
        });
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegActivity.class);
                startActivityForResult(intent,1);


            }
        });

    }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        String et_user=data.getStringExtra("user");
        username.setText(et_user);
    }

    @Override
    void initData() {

    }
    void login(){
        UserModel userModel=new UserModel();
        userModel.login(username.getText().toString().trim(),userpass.getText().toString().trim(),this);
    }
    @Override
    public void onSuccess(UserBean userId,int flag) {
        if(userId.getUserid().equals("0")){
            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }
        else{
            saveUser(userId.getUserid());
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();


    }

//    @Override
//    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.btn_login:
//                if (username.getText().toString().trim().equals("")||password.getText().toString().trim().equals("")){
//                    Toast.makeText(LoginActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
//                }
//                else{
//                    login();
//                }
//                break;
//            case R.id.btn_reg:
//                Intent intent=new Intent();
//                intent.setClass(LoginActivity.this,Login_upActivity.class);
//                startActivity(intent);
//                LoginActivity.this.finish();
////                saveUser();
//                break;
//        }
//
//    }

    public void saveUser(String user_id){
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("username",username.getText().toString());

        editor.putString("user_id",user_id);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();//提交修改
    }

}
